<?php
require_once 'init.php';
$phpmongo = new Phpmongo();
?>

<?php
// create db
if ($_POST['act'] == 'createcollection'){
	$arr = array();
	$arr['db'] = $_POST['db'];
	$arr['collection'] = $_POST['collection'];
	$arr['capped'] = $_POST['capped'];
	$arr['size'] = $_POST['size'];
	$arr['max'] = $_POST['max'];
	$phpmongo->createCollection($arr);
	message("New Collection created.", "db.php?db=$arr[db]");
}
?>
<?php
// drop Collection
if ($_GET['act'] == 'dropCollection'){
	$phpmongo->dropCollection($_GET['db'],$_GET['collection']);
	message("collection $_GET[collection] dropped.", "db.php?db=$_GET[db]");
}
?>
<?php
// clear Collection
if ($_GET['act'] == 'clearCollection'){
	$phpmongo->clearCollection($_GET['db'],$_GET['collection']);
	message("collection $_GET[collection] cleared.", "db.php?db=$_GET[db]");
}
?>
<?php
// insert  row
if ($_POST['act'] == 'insert'){
	$db = $_POST['db'];
	$collection = $_POST['collection'];
	$phpmongo = new Phpmongo($db, $collection);  
	$phpmongo->insert($_POST['data']); 
	message("insert a row sucessful", "collection.php?db=$db&collection=$collection");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/common.js"></script>
<title>PhpMongo</title>
</head>
<body>
<div class="warp_left">
<?php
require_once 'left.php';
?>	
</div>
<?php
$db = $_GET['db'];
$collection = $_GET['collection'];
$act = $_GET['act']? $_GET['act'] : "query"; 
$query = $_GET['query'] ? $_GET['query'] : 'array()';
$limit = $_GET['limit'] ? $_GET['limit'] : 0;
$pagesize = $_GET['pagesize']? $_GET['pagesize']: 30;
?>
<div class="warp_right">
	<div>
		<ul class="collection_menu">
			<li>
				<a href="collection.php?act=query&db=<?php echo $db;?>&collection=<?php echo $collection;?>" 
				<?php if ($act =='query'){echo ' class="actived" ';}?>>Query</a>
			</li>
			<li>
				<a href="collection.php?act=insert&db=<?php echo $db;?>&collection=<?php echo $collection;?>" 
				<?php if ($act =='insert'){echo ' class="actived" ';}?>>Insert</a>
			</li>
			<li>
				<a 
onclick="return window.confirm('Are you sure to drop collecion <?php echo $collection;?>?All data in the collection will be losed!');" 
href="collection.php?act=dropCollection&db=<?php echo $db;?>&collection=<?php echo $collection;?>">Drop</a>
			</li>
			<li>
				<a 
onclick="return window.confirm('Are you sure to clear all records in collection <?php echo $collection;?>?');"
href="collection.php?act=clearCollection&db=<?php echo $db;?>&collection=<?php echo $collection;?>">Clear</a>
			</li>
		</ul>
	</div>
	<hr />
	
	<?php if ($act == 'query'): ?>
	<div>
		<form method="get" action="collection.php">
				<table>
					<tr>
						<td><b>Query</b></td>
					</tr>
					<tr>
						<td>Query *:</td>
						<td>
						<textarea name="query" rows="7" cols="80"><?php echo $_GET['query']?$_GET['query']:'array()';?></textarea>
						</td>
					</tr>
					<tr>
						<td>Limit:</td>
						<td>
						<input type="text" name="limit" value="<?php echo $_GET['limit']?$_GET['limit']:0;?>" />
						</td>
					</tr>
					<tr>
						<td>Rows per Page:</td>
						<td>
						<input type="text" name="pagesize" value="<?php echo $_GET['pagesize']?$_GET['pagesize']:30;?>" />
						</td>
					</tr>
					<tr>
						<td>
						<input type="hidden" name="db" value="<?php echo $db;?>" />
						<input type="hidden" name="collection" value="<?php echo $collection;?>" />
						<input type="hidden" name="act" value="query" />
						<input type="submit" value="Query"  class="submit" /></td>
					</tr>
				</table>		
		</form>
	</div>
	<div class="result_list">
<?php
		$phpmongo = new Phpmongo($db, $collection);
		$p = $_GET['p']? $_GET['p'] : 1;
		$pagesize = $_GET['pagesize'] ? $_GET['pagesize'] : 30; 
		$limit = $_GET['limit']? $_GET['limit'] : 0; 
		$skip = ($p-1)*$pagesize-$limit;
		//char change into Array For query
		$query = $phpmongo->stringToArray($query);;
		$result = $phpmongo->query($query,array('limit'=>$pagesize,'skip'=>$skip));
		$count = $phpmongo->_count; 
		$total = ceil($count/$pagesize);
?>
		<table class="table_info">
		<?php
		if(is_array($result) && !empty($result)):
		?>
		<tr>
			<th>Action</th>
			<th>Result</th>
		</tr>
		<?php
		endif;
		?>	
		<?php
		foreach ($result as $row):
		?>
			<tr>
				<td width="50">
				<a href="<?php echo 'document.php?act=editDocument&db='.$db.'&collection='.$collection.'&id='.$row['_id'];?>"><img src="images/ico-edit.gif" /></a>
				<a href="<?php echo 'document.php?act=remove&db='.$db.'&collection='.$collection.'&id='.$row['_id'];?>"
onclick="return window.confirm('Are you sure to delete this document?');" 				
				><img src="images/ico-delete.gif" /></a>
				</td>
				<td><?php var_export($row);?></td>
			</tr>
		<?php
		endforeach;
		?>	
		<tr>
			<td>
			Page:
			</td>
			<td class="page">
<?php
$url = "collection.php?db=$db&collection=$collection&query=$query&limit=$limit&pagesize=$pagesize&p=";
$page = new Page($total,$url);
echo $page->show();
?>
			</td>
		</tr>
		</table>
	</div>	
	<?php endif;?>
	
	<?php if ($act == 'insert'): ?>
	<div>
		<form method="POST" action="collection.php?db=<?php echo $db;?>&collection=<?php echo $collection;?>">
				<table>
					<tr>
						<td><b>Insert</b></td>
					</tr>
					<tr>
						<td>Query *:</td>
						<td>
						<textarea name="data" rows="7" cols="80">array()</textarea>
						</td>
					</tr>
					<tr>
						<td>Option:</td>
						<td>
						Safe<input type="checkbox" name="option" value="1" />
						Fsync<input type="checkbox" name="option" value="0" />
						</td>
					</tr>

					<tr>
						<td>
						<input type="hidden" name="db" value="<?php echo $db;?>" />
						<input type="hidden" name="collection" value="<?php echo $collection;?>" />
						<input type="hidden" name="act" value="insert" />
						<input type="submit" value="Insert"  class="submit" /></td>
					</tr>
				</table>		
		</form>
	</div>		
	<?php endif;?>
	

</div>

</body>
</html>